package com.doyens.seckill.controller;

import com.doyens.auth.entity.UserInfo;
import com.doyens.seckill.interceptor.LoginInterceptor;
import com.doyens.seckill.service.SeckillOrderService;
import com.doyens.seckill.utils.Result;
import com.doyens.seckill.utils.SeckillStatus;
import com.mysql.jdbc.Statement;
import com.mysql.jdbc.log.LogFactory;
import org.apache.ibatis.javassist.bytecode.stackmap.BasicBlock;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;

@RestController
@RequestMapping("/seckill/order")
public class SeckillOrderController {

    @Autowired
    private SeckillOrderService seckillOrderService;


    private static  final Logger  LOGGER = LoggerFactory.getLogger(SeckillOrderController.class);

    @GetMapping("/add")
    public Result add(String time, Long id){
        UserInfo  userInfo = LoginInterceptor.getUserInfo();
        try {
            if (userInfo == null){
                return new Result(403,"用户未登录!");
            }
            Boolean boll = seckillOrderService.add(id, time, userInfo.getUsername());
            if (boll){
                return new Result(0,"抢单成功");
            }else{
                return new Result(401,"下单失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            return new Result(2,e.getMessage());
        }

    }

    //查询用户抢单状态
    @GetMapping("/queryStatus")
    public Result queryStatus(){

        try {
            UserInfo  userInfo = LoginInterceptor.getUserInfo();
            if (userInfo == null){
                return new Result(403,"用户未登录!!");
            }
            SeckillStatus seckillStatus = seckillOrderService.queryStatus(userInfo.getUsername());
            if (seckillStatus !=null){
                return new Result(seckillStatus.getStatus(),"抢单状态");
            }
        }catch (Exception e){
            e.printStackTrace();
            return new Result(0,e.getMessage());
        }

        return new Result(404,"无相关信息!!!");

    }





}
